<!--
 * 字典管理
 * @author: hexiaozhen
 * @since: 2023-09-27
 * index.vue
-->
<template>
  <div v-show="$route.name === 'Dict'" class="main-content">
    <!-- 搜索 -->
    <TableForm
      ref="formAndTableRef"
      :formItems="formItems"
      :formData="formData"
      :girdOption="girdOption"
      class="tableForm"
      @getTableList="getTableList"
      @selectionChange="selectionChange"
      @handleBtnClick="handleBtnClick"
    >
      <!-- 自定义操作按钮 -->
      <template #tableOptate="{ record }">
        <a-button
          v-for="clickItem in tableOperateList"
          :key="clickItem.text"
          type="text"
          class="operate-item"
          :status="clickItem.eventName == 'tableDel' ? 'danger' : ''"
          @click="handleBtnClick(clickItem.eventName, record)"
        >
          {{ clickItem.text }}
        </a-button>
      </template>
      <!-- 列插槽 -->
      <template #dictType="{ record }">
        <span class="dictType" @click="dictTypeClick(record.dictType, record.dictName)">
          {{ record.dictType }}
        </span>
      </template>
      <template #createTime="{ record }">
        {{ record.createTime.slice(0, 10) }}
      </template>
      <template #status="{ record }">
        {{ record.status === '1' ? '停用' : '开启' }}
      </template>
    </TableForm>
    <AddEditDict v-if="visible" v-model:visible="visible" :rowData="rowData" @submit="getTableList()"></AddEditDict>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue'
import TableForm from '@/components/tableForm/index.vue'
import AddEditDict from './components/addEditDict.vue' // 新增编辑
import system from '@/api/modules/system'
import { Modal, Message } from '@arco-design/web-vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const formData = reactive({ dictName: '', dictType: '', dateRange: ['', ''], status: '' })
const formAndTableRef = ref()
// 获取字典数据
const statusOptions = ref([])
// 表单配置
const formItems = reactive([
  { name: 'dictName', label: '字典名称', type: 'a-input' },
  { name: 'dictType', label: '字典类型', type: 'a-input' },
  { name: 'status', label: '状态', type: 'a-select', options: statusOptions },
  { name: 'dateRange', label: '创建时间', type: 'a-range-picker' },
])
const girdOption = reactive({
  btnList: [
    { name: '新增', eventName: 'add', type: 'icon-xinzeng' },
    { name: '导出', eventName: 'export', type: 'icon-daochu' },
    { name: '删除', status: 'danger', eventName: 'delMultiple', type: 'icon-shanchu' },
  ],
  columns: [
    { title: '序号', slotName: 'serialNumber', width: 70 },
    { title: '字典名称', dataIndex: 'dictName', width: 100 },
    { title: '字典类型', slotName: 'dictType', width: 180 },
    { title: '状态', slotName: 'status', width: 70 },
    { title: '备注', dataIndex: 'remark', width: 150 },
    { title: '创建时间', slotName: 'createTime', width: 124 },
    { title: '操作', slotName: 'optional', width: 110, fixed: 'right' },
  ],
  loading: false,
  tableSlotList: ['dictType', 'createTime', 'status'], //表格插槽
  tableId: 'dictId', //表格唯一标识符类似于id
  rowSelection: true, // 多选
})

// 表格操作插槽
const tableOperateList = reactive([
  { text: '修改', eventName: 'tableEdit' },
  { text: '删除', eventName: 'tableDel' },
])
let visible = ref(false) // 新增编辑弹窗
let rowData = reactive({ type: 'add' }) // 弹窗数据
let selectionList = [] // 多选数据

const handleBtnClick = (type, row) => {
  switch (type) {
    case 'add': {
      // 新增
      rowData = { type: 'add' }
      visible.value = true
      break
    }
    case 'delMultiple': {
      //批量删除
      if (!selectionList.length) {
        return Message.warning('请先选择表格数据！')
      }
      Modal.open({
        title: '警告',
        content: `是否确认批量删除已选数据项？`,
        draggable: true,
        onOk: () => {
          delDict(selectionList.join(','))
        },
      })
      break
    }
    case 'tableEdit': {
      //编辑
      visible.value = true
      rowData = { ...row, type: 'edit' }
      break
    }
    case 'export': {
      // 导出
      break
    }

    case 'tableDel': {
      //删除
      Modal.open({
        title: '警告',
        content: `是否确认删除字典名称为${row.dictName}的数据项？`,
        draggable: true,
        onOk: () => {
          delDict(row.dictId)
        },
      })
      break
    }
  }
}
// 删除
const delDict = (id) => {
  system.delDict(id).then((res) => {
    getTableList()
    Message.success(res.msg)
  })
}
const dictTypeClick = (dictType, dictName) => {
  router.push({ path: '/dictType', query: { dictType: dictType, dictName: dictName } })
}
// 获取表格数据
let requestParam = {}
const getTableList = (pageConfig) => {
  if (pageConfig) {
    requestParam.size = pageConfig.pageSize
    requestParam.current = pageConfig.current
  }
  let param = { ...formData }
  param.createTimeStart = param.dateRange[0].length ? param.dateRange[0] + ' 00:00:00' : ''
  param.createTimeStartEnd = param.dateRange[1].length ? param.dateRange[1] + ' 23:59:59' : ''
  delete param.dateRange
  girdOption.loading = true
  system
    .getDictList(param, requestParam)
    .then((res) => {
      formAndTableRef.value.setTableData(res.data.records, Number(res.data.total))
      girdOption.loading = false
    })
    .catch(() => {
      girdOption.loading = false
    })
}
// 多选
const selectionChange = (list) => {
  selectionList = list
}
// 数据字典
const getOption = () => {
  const param = { dictTypes: 'sys_normal_disable' }
  system.getDicts(param).then((res) => {
    statusOptions.value = res.data.sys_normal_disable.map((it) => ({
      label: it.dictLabel,
      value: it.dictValue,
    }))
  })
}
onMounted(() => {
  getOption()
  getTableList({ pageSize: 10, current: 1 })
})
</script>
<style scoped>
.dictType {
  color: rgb(var(--arcoblue-6));
  cursor: pointer;
}
</style>
